require("nvchad.configs.lspconfig").defaults()

local servers = { "html", "cssls", "clangd", "cmake", "ts_ls", "tsserver", "pyright" }
vim.lsp.enable(servers)

-- read :h vim.lsp.config for changing options of lsp servers
--
-- vim.lsp.config("clangd", {
--   on_attach = function(client, bufnr)
--     -- local opts = { noremap = true, silent = true }
--     -- vim.keymap.set("n", "<space>s", "<cmd>ClangdSwitchSourceHeader<cr>", "switch to source/header")
--     vim.keymap.set("n", "<leader>s", "<cmd>LspClangdSwitchSourceHeader<cr>", "switch to source/header")
--     -- vim.keymap.set("n", "<space>o", "<cmd>ClangdShowSymbolInfo<cr>", opts)
--   end,
-- })

vim.api.nvim_create_autocmd("LspAttach", {
  callback = function(e)
    local opts = { buffer = e.buf }

    vim.bo[e.buf].formatexpr = nil
    vim.bo[e.buf].omnifunc = nil

    local client = vim.lsp.get_client_by_id(e.data.client_id)
    if client.server_capabilities.signatureHelpProvider then
      -- require('lsp-overloads').setup(client, { })
    end
    vim.schedule(function()
      -- vim.keymap.del("n", "<leader>ra")
      -- vim.keymap.del("n", "<leader>D")
    end)

    vim.keymap.set("n", "<leader>a", vim.lsp.buf.code_action, { desc = "lsp code action" })
    vim.keymap.set("n", "<leader>d", vim.lsp.buf.definition, { desc = "lsp goto definition" })
    vim.keymap.set("i", "<C-;>", vim.lsp.buf.signature_help, { desc = "lsp signature hover" })
    vim.keymap.set("n", "<leader>s", "<cmd>LspClangdSwitchSourceHeader<cr>", { desc = "lsp source/header" })
    vim.keymap.set("n", "<leader>n", vim.diagnostic.goto_next, { desc = "lsp diagnostic next" })
    vim.keymap.set("n", "<leader>N", vim.diagnostic.goto_prev, { desc = "lsp diagnostic prev" })

    vim.keymap.set("n", "<leader>L", vim.diagnostic.setloclist, { desc = "lsp diagnostic loclist" })
    -- vim.keymap.set("n", '<space>e', function () vim.lsp.diagnostic.show_line_diagnostics () end, opts)
  end,
})
